package System_code;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-07-26
 * Time: 22:12
 */
public class Demo6 {
    // 1. 单个线程, 串行的, 完成 20 亿次自增.
    // 2. 两个线程, 并发的, 完成 20 亿次自增.
    private static final long COUNT = 20_0000_0000;
    private static void serial() {
        long begin=System.currentTimeMillis();
        int a=0;
        for(int i=0;i<COUNT;i++){
            a++;
        }
        a=0;
        for(int i=0;i<COUNT;i++){
            a++;
        }
        long end=System.currentTimeMillis();
        System.out.println("单个线程完成20亿自增的时间"+(end-begin)+"ms");
    }
    private static void concurrency() {
        long begin=System.currentTimeMillis();
        Thread t1=new Thread(()->{
            int a=0;
            for(int i=0;i<COUNT;i++){
                a++;
            }
        });
        Thread t2=new Thread(()->{
            int a=0;
            for(int i=0;i<COUNT;i++){
                a++;
            }
        });
        t1.start();
        t2.start();
        long end=System.currentTimeMillis();
        System.out.println("多线程执行20亿次相加的时间"+(end-begin)+"ms");
    }
    public static void main(String[] args) {
    serial();
    concurrency();
    }
}
